<style>
    .layui-layer-title {
        display: flex;
        align-items: center;
    }
</style>

<div class="layui-fluid">
    <div class="layui-card">
        <div class="layui-card-body">
            <!-- 头部操作栏 -->
            <div class="layui-form lay-header">
                <div class="toolbar">
                    <div class="layui-form-item lay-query-content">
                        <div class="lay-query-box">
                            <div class="query-lt">
                                <div class="layui-inline">
                                    <label class="layui-form-label">质检编号</label>
                                    <div class="layui-input-inline mr0">
                                        <input name="code" class="layui-input" type="text" placeholder="请输入质检编号" />
                                    </div>
                                </div>
                                <div class="layui-inline">
                                    <label class="layui-form-label">报工编号</label>
                                    <div class="layui-input-inline mr0">
                                        <input name="processingReportCode" class="layui-input" type="text" placeholder="请输入报工编号" />
                                    </div>
                                </div>
                                <div class="layui-inline">
                                    <label class="layui-form-label">生产计划编号</label>
                                    <div class="layui-input-inline mr0">
                                        <input name="productionPlanCode" class="layui-input" type="text" placeholder="请输入生产计划编号" />
                                    </div>
                                </div>
                                <div class="layui-inline">
                                    <label class="layui-form-label">制造令编号</label>
                                    <div class="layui-input-inline mr0">
                                        <input name="productionPlanDetailCode" class="layui-input" type="text" placeholder="请输入制造令编号" />
                                    </div>
                                </div>
                                <div class="layui-inline">
                                    <label class="layui-form-label w-auto">产线</label>
                                    <div class="layui-input-inline mr0">
                                        <select id="productionLineId" name="productionLineId" lay-filter="productionLineId" lay-search required></select>
                                    </div>
                                </div>
                                <div class="layui-inline">
                                    <label class="layui-form-label">成品捆包号</label>
                                    <div class="layui-input-inline mr0">
                                        <input name="finishedProductBundleCode" class="layui-input" type="text" placeholder="请输入捆包号" />
                                    </div>
                                </div>
                                <div class="layui-inline">
                                    <label class="layui-form-label">客户名称</label>
                                    <div class="layui-input-inline mr0">
                                        <input name="customerName" class="layui-input" type="text"
                                               placeholder="请输入客户名称"/>
                                    </div>
                                </div>
<!--                                <div class="layui-inline">-->
<!--                                    <label class="layui-form-label">原始捆包号</label>-->
<!--                                    <div class="layui-input-inline mr0">-->
<!--                                        <input name="userBundleCode" class="layui-input" type="text"-->
<!--                                               placeholder="请输入捆包号"/>-->
<!--                                    </div>-->
<!--                                </div>-->
                            </div>
                            <div class="query-rt">
                                <div class="layui-inline">
                                    <button class="layui-btn icon-btn color-green" lay-filter="formSubSearchLog" lay-submit layui-form-keyDownSearch="true">查询</button>
                                    <button class="layui-btn icon-btn color-white" lay-filter="resets" lay-submit>重置</button>
                                </div>
                            </div>
                        </div>
                    </div>
                </div>


                <div class="lay-spread-box">
                    <div class="spread-btn">
                        <span class="spread-span">展开</span>
                        <i class="spread-more"></i>
                    </div>
                </div>

                <div class="lay-btn-rows">
                    <div class="lay-btn-box">
                        <div class="lay-btn-lt">
                            <button id="ProductionOrderBtnAdd" class="layui-btn icon-btn color-reseda">添加</button>
                        </div>
                    </div>
                </div>
            </div>

            <!-- 表格 -->
            <table class="layui-table" id="ProductionOrderTable" lay-filter="ProductionOrderTable"></table>
        </div>
    </div>
</div>

<script type="text/html" id="ProductionOrderTableBar">
    <a class="layui-btn layui-btn-xs layui-btn" lay-event="detail">详情</a>
    <a class="layui-btn layui-btn-danger layui-btn-xs" lay-event="delete">删除</a>
</script>

<script>
    layui.use(["layer", "form", "util", "table", "admin", "laytpl", "laydate", "tableTreeDj"], function () {
        var $ = layui.jquery;
        var layer = layui.layer;
        var form = layui.form;
        var table = layui.table;
        var admin = layui.admin;
        var config = layui.config;

        // 初始化车间下拉框
        // admin.initDataSelectPro(
        //     "warehouse/warehouse/productionLine/list?page=1&limit=9999",  // 替换为实际请求地址
        //     { limit: 9999 },
        //     "workshopId",  // 目标下拉框ID
        //     "车间",  // 下拉框标题
        //     null,
        //     "workshopName"
        // );

        // 初始化产线下拉框
        admin.initDataSelectPro(
            "warehouse/warehouse/productionLine/list?page=1&limit=9999",  // 替换为实际请求地址
            { limit: 9999 },
            "productionLineId",  // 目标下拉框ID
            "产线",  // 下拉框标题
            null,
            "productionLineName"
        );

        // 渲染表格
        var insTb = table.render({
            elem: "#ProductionOrderTable",
            url: config.base + "warehouse/warehouse/qualityTesting/list",
            toolbar: true,
            defaultToolbar: ["filter"],
            where: { access_token: config.getToken().access_token },
            page: true,
            cellMinWidth: 100,
            cols: [
                [
                    { type: "numbers", title: "序号" },
                    { field: "code", align: "center", title: "质检编号" },
                    { field: "processingReportCode", align: "center", title: "报工记录编号" },
                    { field: "computerPracticeCode", align: "center", title: "上机记录编号" },
                    {field: "customerName", align: "center", title: "客户名称"},
                    {field: "userBundleCode", align: "center", title: "捆包号"},
                    { field: "productionLineName", align: "center", title: "所属产线" },
                    { field: "qualityTestingCategoryName", align: "center", title: "质检分类" },
                    { field: "qualityTestingTypeName", align: "center", title: "质检类型" },
                    // {
                    //     field: "status",
                    //     align: "center",
                    //     title: "状态",
                    //     templet: function (d) {
                    //         return d.status == 1 ? "待审核" : "生产中";
                    //     }
                    // },
                    { field: "createTime", align: "center", title: "创建时间" },
                    { align: "center", fixed: "right", toolbar: "#ProductionOrderTableBar", fixed: "right",title: "操作", width: 160 }
                ]
            ]
        });

        // 工具条点击事件
        table.on("tool(ProductionOrderTable)", function (obj) {
            var data = obj.data;
            var layEvent = obj.event;

            if (layEvent === "detail") {
                admin.req(
                    "warehouse/warehouse/qualityTesting/selectQualityTesting",
                    { id: data.id },
                    function (res) {
                        if (res.code === 200) {
                            openFormFun("detail", res.data);
                        }
                    }
                );
            } else if (layEvent === "delete") {
                layer.open({
                    title: "操作",
                    content: "是否删除记录？",
                    btn: ["确认删除", "关闭"],
                    yes: function (index) {
                        admin.req(
                            "warehouse/warehouse/qualityTesting/deleteQualityTesting",
                            { id: data.id },
                            function (res) {
                                if (res.code === 200) {
                                    layer.msg("删除成功");
                                    layer.close(index);
                                    insTb.reload();
                                }
                            }
                        );
                    }
                });
            } else if (layEvent === "edit") {
                admin.req(
                    "warehouse/warehouse/qualityTesting/selectQualityTesting",
                    { id: data.id },
                    function (res) {
                        if (res.code === 200) {
                            openFormFun("edit", res.data);
                        }
                    }
                );
            } else if (layEvent === "audit") {
                layer.open({
                    title: "审核",
                    content: "是否审核通过？",
                    btn: ["审核通过", "关闭"],
                    yes: function (index) {
                        admin.req(
                            "warehouse/warehouse/productionSchedulingPlan/updateProductionSchedulingPlan",
                            { id: data.id, status: 2 },
                            function (res) {
                                if (res.code === 200) {
                                    layer.msg("审核通过");
                                    layer.close(index);
                                    insTb.reload();
                                }
                            }
                        );
                    }
                });
            }
        });

        // 搜索
        form.on("submit(formSubSearchLog)", function (data) {
            insTb.reload({ where: data.field, page: { curr: 1 } }, "data");
        });

        // 重置搜索
        form.on("submit(resets)", function (data) {
            insTb.reload({ where: admin.resetSearch(data.field), page: { curr: 1 } }, "data");
        });

        form.render("select");

        // 添加按钮点击事件
        $("#ProductionOrderBtnAdd").click(function () {
            openFormFun("add");
        });

        function openFormFun(eventName, data) {
            admin.formOpen({
                area: "90%",
                offset: "5%",
                title: "添加加工质检记录",
                path: "components/warehouse/product/warehouseQualityTestingForm.html",
                success: function () {
                    parentData.eventName = eventName;
                    parentData.data = data;
                }
            });
        }
    });
</script>
